function obj2qs(obj, isAddEmpty, hasQs = false) {
  obj = obj || {};
  var qs = hasQs ? '&' : '?'
  Object.keys(obj).forEach(key => {
    let value = obj[key]
    if (key && (isAddEmpty || value)) {
      qs.length !== 1 && (qs += '&')
      qs += key + '=' + value
    }
  })
  return qs.length > 1 ? qs : ''
}

export default (url, methods, datas) => {
  return new Promise((resolve, reject) => {
    let type;
    let data;
    let method;
    type = methods === "POST" ? 'application/x-www-form-urlencoded' : 'application/json';
    if (datas && (methods === 'POST' || methods === 'POST_J')) {
      data = datas;
      methods = 'POST'
    }
    if (datas && methods === 'GET') {
      url += obj2qs(datas, true)
    }
    method = methods;
    wx.request({
      url,
      method,
      data,
      header: {
        "Authorization": 'Bearer ' + (wx.getStorageSync('token') || ""), // 登录后获取的token
        'content-type': type // 默认值
      },
      success(res) {
        const data = res.data;
        if (data.status === '0') {
          resolve(data)
        } else {
          reject()
        }
      },
      fail(err) { // 请求失败后判断状态码，如果是登录问题就去登录
        wx.hideLoading()
        reject(err)
      }
    })
  })
}